package com.cxrj.zhsq.interfaceManage.controller;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cxrj.zhsq.redCommunity.service.VolunteerRecruitService;
import com.cxrj.zhsq.utils.IpUtil;

@Controller
@RequestMapping(value = "/interface")
public class InterfaceVolunteerController {

	@Resource
	private VolunteerRecruitService volunteerRecruitService;

	/**
	 * 分页查询志愿者招募信息
	 * 
	 * @return
	 */
	@RequestMapping(value = "selectRecruit", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
	@ResponseBody
	public Map<String, Object> selectRecruit(String dataIndex, String pageCount, HttpServletRequest request) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		try {
			resultMap = volunteerRecruitService.selectVolunteerRecrui(dataIndex, pageCount);
			resultMap.put("path", "http://" + IpUtil.getIpAddr(request) + ":" + request.getServerPort() + "/zhsq_xg/");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return resultMap;
	}

	/**
	 * 分页查询志愿者招募信息
	 * 
	 * @return
	 */
	@RequestMapping(value = "selectRecruitByMy", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
	@ResponseBody
	public Map<String, Object> selectRecruitByMy(String userId, String status, String dataIndex, String pageCount, HttpServletRequest request) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		try {
			resultMap = volunteerRecruitService.selectVolunteerRecruiByMy(userId, status, dataIndex, pageCount);
			resultMap.put("path", "http://" + IpUtil.getIpAddr(request) + ":" + request.getServerPort() + "/zhsq_xg/");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return resultMap;
	}

	/**
	 * 根据id查询志愿者招募信息
	 * 
	 * @return
	 */
	@RequestMapping(value = "selectRecruitId", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
	@ResponseBody
	public Map<String, Object> selectRecruitId(String id, String userId, HttpServletRequest request) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		try {
			resultMap = volunteerRecruitService.selectVolunteerRecruitById(id, userId, request);
			resultMap.put("path", "http://" + IpUtil.getIpAddr(request) + ":" + request.getServerPort() + "/zhsq_xg/");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return resultMap;
	}

	/**
	 * 志愿者招募报名
	 */
	@RequestMapping(value = "insertEnroll", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
	@ResponseBody
	public Map<String, Object> insertEnroll(String id, String userId, HttpServletRequest request) {
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			map = volunteerRecruitService.insertVolunteerEnroll(id, userId);
		} catch (Exception e) {
			map.put("result", "0");
			e.printStackTrace();
		}
		return map;
	}

	/**
	 * 志愿者招募取消报名
	 */
	@RequestMapping(value = "delEnroll", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
	@ResponseBody
	public Map<String, Object> delEnroll(String id, String userId, HttpServletRequest request) {
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			map = volunteerRecruitService.updateVolunteerEnroll(id, userId);
		} catch (Exception e) {
			map.put("result", "0");
			e.printStackTrace();
		}
		return map;
	}

	/**
	 * 分页查询志愿者风采
	 * 
	 * @return
	 */
	@RequestMapping(value = "selectMien", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
	@ResponseBody
	public Map<String, Object> selectMien(String name, String dataIndex, String pageCount, HttpServletRequest request) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		try {
			resultMap = volunteerRecruitService.selectVolunteerMien(name, dataIndex, pageCount);
			resultMap.put("path", "http://" + IpUtil.getIpAddr(request) + ":" + request.getServerPort() + "/zhsq_xg/");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return resultMap;
	}
}
